package com.demo.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.dao.ProjectDao;
import com.demo.model.Project;
import com.demo.service.ProjectService;

@Service
public class ProjectServiceImpl implements ProjectService {

	@Autowired
	private ProjectDao projectDao;

	@Override
	public List<Project> getProjects() {
		return projectDao.select(null);
	}

	@Override
	public int save(Project project) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		project.setCreateTime(formatter.format(LocalDateTime.now()));
		return projectDao.insert(project);
	}

	@Override
	public Integer deleteProject(int id) {
		return projectDao.delete(id);
	}

	@Override
	public Integer updateProject(Project project) {
		if (project.getId() != null && project.getId() > 0) {
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			project.setModifyTime(formatter.format(LocalDateTime.now()));
			return projectDao.update(project);
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		project.setCreateTime(formatter.format(LocalDateTime.now()));
		return projectDao.insert(project);

	}

	@Override
	public List<Project> getProjects(String name) {
		return projectDao.select(name);
	}

	@Override
	public Project getProjectById(int id) {
		return projectDao.selectOne(id);
	}
}
